package com.fafa.pos.util;

import android.app.Activity;
import android.app.Application;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZhouBing on 2017/7/19.
 *
 * 管理当前所有的activity
 */
public class MyActivityManager extends Application{

    private Activity loginActivity;

    private List<Activity> activities = new ArrayList<>();

    private static MyActivityManager manager ;

    private MyActivityManager(){};

    public static MyActivityManager getInstance(){
        if(manager==null){
            manager = new MyActivityManager();
        }
        return  manager;
    }

    public void setLoginActivity(Activity loginActivity) {
        this.loginActivity = loginActivity;
    }

    public void addActivity(Activity activity){
        activities.add(activity);
    }


    /**
     * 结束当前所有activity
     */
    public void reLogin(){
        if(activities!=null && activities.size()>0){

            for (Activity activity:activities) {
                if(activity!=null){
                    if(!activity.isFinishing()){
                        activity.finish();
                    }
                    activity = null;
                }
            }
        }
    }

    /**
     * 完全退出退出
     */
    public void exit(){
        if(activities!=null && activities.size()>0){

            for (Activity activity:activities) {
                if(activity!=null){
                    if(!activity.isFinishing()){
                        activity.finish();
                    }
                    activity = null;
                }
            }
        }
        System.exit(0);
    }


}
